/*
 * ContactDetailTypeValueDisplay.java
 *
 * Created on 07 January 2004, 02:19
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.contactdetail;

import com.velocityme.valueobjects.NotificationMechanismValue;
import java.util.Collection;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;

/**
 *
 * @author  Robert Crida Work
 */
public class ContactDetailTypeValueDisplay extends javax.swing.JPanel {
    
    private ContactDetailTypeTableModel m_modelContactDetailType;
    private int m_selectedRow;
    private boolean m_editable = false;
    
    /** Creates new form ContactDetailTypeValueDisplay */
    public ContactDetailTypeValueDisplay(NotificationMechanismValue[] p_allNotificationMechanismValues) {
        initComponents();
        jTableTypes.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(new JComboBox(p_allNotificationMechanismValues)));
    }
    
    public void setContactDetailTypeValues(Collection p_contactDetailTypeValues) {
        m_modelContactDetailType.addValues(p_contactDetailTypeValues);
    }
    
    public Collection getContactDetailTypeValues() {
        return m_modelContactDetailType.getElements();
    }
    
    public void setEditable(boolean p_editable, NotificationMechanismValue p_defaultNotificationMechanism) {
        m_editable = p_editable;
        m_modelContactDetailType.setEditable(p_editable, p_defaultNotificationMechanism);
    }
    
    private void checkPopup(java.awt.event.MouseEvent e) {
        if (m_editable && e.isPopupTrigger()) {
            m_selectedRow = jTableTypes.getSelectedRow();
            jMenuItemDelete.setEnabled(true);
            jMenuItemInsertAbove.setEnabled(true);
            jMenuItemMoveUp.setEnabled(true);
            jMenuItemMoveDown.setEnabled(true);
            if (m_selectedRow < 0) {
                jMenuItemDelete.setEnabled(false);
                jMenuItemInsertAbove.setEnabled(false);
                jMenuItemMoveUp.setEnabled(false);
                jMenuItemMoveDown.setEnabled(false);
            }
            if (m_selectedRow == 0)
                jMenuItemMoveUp.setEnabled(false);
            if (m_selectedRow == m_modelContactDetailType.getRowCount()-1)
                jMenuItemMoveDown.setEnabled(false);
            jPopupMenu1.show(e.getComponent(), e.getX(), e.getY());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItemInsertAbove = new javax.swing.JMenuItem();
        jMenuItemInsertBelow = new javax.swing.JMenuItem();
        jMenuItemDelete = new javax.swing.JMenuItem();
        jMenuItemMoveUp = new javax.swing.JMenuItem();
        jMenuItemMoveDown = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        m_modelContactDetailType = new ContactDetailTypeTableModel();
        jTableTypes = new javax.swing.JTable(m_modelContactDetailType);

        jMenuItemInsertAbove.setText("Insert Above");
        jMenuItemInsertAbove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemInsertAboveActionPerformed(evt);
            }
        });

        jPopupMenu1.add(jMenuItemInsertAbove);

        jMenuItemInsertBelow.setText("Insert Below");
        jMenuItemInsertBelow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemInsertBelowActionPerformed(evt);
            }
        });

        jPopupMenu1.add(jMenuItemInsertBelow);

        jMenuItemDelete.setText("Delete");
        jMenuItemDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemDeleteActionPerformed(evt);
            }
        });

        jPopupMenu1.add(jMenuItemDelete);

        jMenuItemMoveUp.setText("Move Up");
        jMenuItemMoveUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemMoveUpActionPerformed(evt);
            }
        });

        jPopupMenu1.add(jMenuItemMoveUp);

        jMenuItemMoveDown.setText("Move Down");
        jMenuItemMoveDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemMoveDownActionPerformed(evt);
            }
        });

        jPopupMenu1.add(jMenuItemMoveDown);

        setLayout(new java.awt.BorderLayout());

        jTableTypes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableTypesMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTableTypesMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTableTypesMouseReleased(evt);
            }
        });

        jScrollPane1.setViewportView(jTableTypes);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void jTableTypesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableTypesMouseReleased
        // Add your handling code here:
        checkPopup(evt);        
    }//GEN-LAST:event_jTableTypesMouseReleased

    private void jTableTypesMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableTypesMousePressed
        // Add your handling code here:
        checkPopup(evt);        
    }//GEN-LAST:event_jTableTypesMousePressed

    private void jTableTypesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableTypesMouseClicked
        // Add your handling code here:
        checkPopup(evt);        
    }//GEN-LAST:event_jTableTypesMouseClicked

    private void jMenuItemMoveDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemMoveDownActionPerformed
        // Add your handling code here:
        m_modelContactDetailType.swapValues(m_selectedRow, m_selectedRow+1);        
    }//GEN-LAST:event_jMenuItemMoveDownActionPerformed

    private void jMenuItemMoveUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemMoveUpActionPerformed
        // Add your handling code here:
        m_modelContactDetailType.swapValues(m_selectedRow-1, m_selectedRow);        
    }//GEN-LAST:event_jMenuItemMoveUpActionPerformed

    private void jMenuItemDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDeleteActionPerformed
        // Add your handling code here:
        m_modelContactDetailType.removeRows(m_selectedRow,m_selectedRow);        
    }//GEN-LAST:event_jMenuItemDeleteActionPerformed

    private void jMenuItemInsertBelowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemInsertBelowActionPerformed
        // Add your handling code here:
        m_modelContactDetailType.addValueAt(m_selectedRow+1);        
    }//GEN-LAST:event_jMenuItemInsertBelowActionPerformed

    private void jMenuItemInsertAboveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemInsertAboveActionPerformed
        // Add your handling code here:
        m_modelContactDetailType.addValueAt(m_selectedRow);                
    }//GEN-LAST:event_jMenuItemInsertAboveActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem jMenuItemDelete;
    private javax.swing.JMenuItem jMenuItemInsertAbove;
    private javax.swing.JMenuItem jMenuItemInsertBelow;
    private javax.swing.JMenuItem jMenuItemMoveDown;
    private javax.swing.JMenuItem jMenuItemMoveUp;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableTypes;
    // End of variables declaration//GEN-END:variables
    
}
